package com.ggkt.common.utils;

import java.util.HashMap;
import java.util.Map;
public class ThreadLocalUtils {
    /*使用常量定义一个key*/
    public static final String USER_KEY = "USER_KEY";
    private static final ThreadLocal<Map<String, Object>> THREAD_LOCAL = new ThreadLocal<>();
    /**
     * 存储
     *
     * @param key
     * @param value
     */
    public static void put(String key, Object value) {
        Map<String, Object> map = THREAD_LOCAL.get();
        if (map == null) {
            map = new HashMap<>();
        }
        map.put(key, value);
        THREAD_LOCAL.set(map);
    }

    /**
     * 取值
     *
     * @param key
     */
    public static <T> T get(String key) {
        Map<String, Object> _map = THREAD_LOCAL.get();
        if (_map != null) {
            return (T)_map.get(key);
        }
        return null;
    }
    public static void release() {
        THREAD_LOCAL.remove();
    }
}